package com.cz.modules.jxc.controller;

import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cz.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cz.modules.jxc.entity.JxcSellDetailsEntity;
import com.cz.modules.jxc.service.JxcSellDetailsService;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.R;



/**
 * 
 *
 * @author Cz
 * @email 
 * @date 2020-04-16 13:42:40
 */
@RestController
@RequestMapping("jxc/jxcselldetails")
public class JxcSellDetailsController {
    @Autowired
    private JxcSellDetailsService jxcSellDetailsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("jxc:jxcselldetails:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxcSellDetailsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("jxc:jxcselldetails:info")
    public R info(@PathVariable("id") String id){
        JxcSellDetailsEntity jxcSellDetails = jxcSellDetailsService.getById(id);

        return R.ok().put("jxcSellDetails", jxcSellDetails);
    }

    /**
     * 信息
     */
    @RequestMapping("/getBySellMasterCode/{sellMasterCode}")
    public R getBySellMasterCode(@PathVariable("sellMasterCode") String sellMasterCode){
       List<JxcSellDetailsEntity> sellList=jxcSellDetailsService.list(
               new QueryWrapper<JxcSellDetailsEntity>().eq("sell_master_code",sellMasterCode)
       );
        return R.ok().put("sellList",sellList);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("jxc:jxcselldetails:save")
    public R save(@RequestBody JxcSellDetailsEntity jxcSellDetails){
        jxcSellDetailsService.save(jxcSellDetails);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("jxc:jxcselldetails:update")
    public R update(@RequestBody JxcSellDetailsEntity jxcSellDetails){
        ValidatorUtils.validateEntity(jxcSellDetails);
        jxcSellDetailsService.updateById(jxcSellDetails);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("jxc:jxcselldetails:delete")
    public R delete(@RequestBody String[] ids){
        jxcSellDetailsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 今日单品销量排行
     */
    @RequestMapping("/detailsRanking")
    public R detailsRanking(){
        return R.ok().put("detailsRanking",jxcSellDetailsService.detailsRanking());
    }
    /**
     * 单品销售排行 第三页
     */
    @RequestMapping("/detailsbytime")
    public R detailsbytime(@RequestParam Map<String, Object> params){
            String type= (String) params.get("type");
            String time= (String) params.get("time");
            Date date=new Date();
            String startTime="";
            String endTime="";
            if(!"".equals(time) && time !=null){//有自定义时间
                if("0".equals(type)){//该自定义时间 的当天的起止时间的数据
                    startTime =time+" 00:00:00";
                    endTime =time+" 23:59:59";
                }else if("1".equals(type)){//该自定义时间 的当月的起止时间的数据
                    startTime =time+"-01 00:00:00";
                    Date dd= DateUtil.parse(startTime);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(dd);
                    calendar.add(Calendar.MONTH, 1);
                    calendar.set(Calendar.DAY_OF_MONTH, 0);
                    endTime = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                    endTime +=" 23:59:59";
                }
            }else {//无自定义时间
                if("0".equals(type)){//当天的数据
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    calendar.add(Calendar.HOUR_OF_DAY, 1);//1小时后
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss");
                    startTime=DateUtil.format(date,"yyyy-MM-dd");
                    startTime +=" 00:00:00";
                }else if("1".equals(type)){//当月的数据
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    // 获取前月的第一天
                    calendar.add(Calendar.MONTH, 0);
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    startTime= DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                    startTime +=" 00:00:00";
                    // 获取当前月的最后一天
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(date);
                    calendar1.add(Calendar.MONTH, 1);
                    calendar1.set(Calendar.DAY_OF_MONTH, 0);
                    endTime = DateUtil.format(calendar1.getTime(),"yyyy-MM-dd");
                    endTime +=" 23:59:59";
                }
            }
        return R.ok().put("detailsRanking",jxcSellDetailsService.detailsbytime(startTime,endTime));
    }
    /**
     * 该订单下的交易记录
     */
    @RequestMapping("/querydetail")
    public R querydetail(@RequestParam Map<String, Object> params){
        return R.ok().put("querydetail",jxcSellDetailsService.querydetail(params));
    }

    /**
     * 销售量Top10
     */
    @RequestMapping("/xslTop10")
    public R xslTop10(@RequestParam Map<String, Object> params){
        String type= (String) params.get("type");
        String startTime= (String) params.get("startTime");
        String endTime= (String) params.get("endTime");
        Calendar calendar = Calendar.getInstance();
        if("0".equals(type)){ //近7天
            endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            calendar.add(Calendar.DAY_OF_MONTH, -6);
            startTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
        }else if("1".equals(type)){//近30天
            endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            calendar.add(Calendar.DAY_OF_MONTH, -29);
            startTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
        }
        return R.ok().put("chartList",jxcSellDetailsService.xslTop10(startTime,endTime));
    }

    /**
     * 该订单下的交易记录
     */
    @RequestMapping("/xseTop10")
    public R xseTop10(@RequestParam Map<String, Object> params){
        String type= (String) params.get("type");
        String startTime= (String) params.get("startTime");
        String endTime= (String) params.get("endTime");
        Calendar calendar = Calendar.getInstance();
        if("0".equals(type)){ //近7天
            endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            calendar.add(Calendar.DAY_OF_MONTH, -6);
            startTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
        }else if("1".equals(type)){//近30天
            endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
            calendar.add(Calendar.DAY_OF_MONTH, -29);
            startTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
        }
        return R.ok().put("chartList",jxcSellDetailsService.xseTop10(startTime,endTime));
    }
}
